package com.qingmi.farmer.ui.order.goods;


import android.view.View;

import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.DataSetting;
import com.qingmi.farmer.data.UserData;
import com.qingmi.farmer.data.model.GoodsOrder;
import com.qingmi.farmer.data.repository.data.GoodsDataRepository;
import com.qingmi.farmer.databinding.FragmentGoodsOrderConfirmedBinding;
import com.qingmi.farmer.event.GoodsOrderDataChangeEvent;
import com.qingmi.farmer.event.UserLoginChangeEvent;
import com.wjjun.base.ui.base.ListFragment;
import com.qingmi.farmer.viewmodel.GoodsOrderItemViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 已确认的生资订单
 */
public class GoodsOrderConfirmedFragment extends ListFragment<GoodsOrderConfirmedViewModel, FragmentGoodsOrderConfirmedBinding> {


    @Inject
    GoodsDataRepository goodsDataRepository;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_goods_order_confirmed;
    }

    @Override
    protected GoodsOrderConfirmedViewModel initViewModel() {
        return new GoodsOrderConfirmedViewModel();
    }

    @Override
    protected void initContent() {
        goodsDataRepository.bind(this);
        super.initContent();
        commListViewModel.setEnableLoadMore(false);
        commListViewModel.setEmpty(getString(R.string.prompt_no_order), R.drawable.ic_no_order);
        RxBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataChange(GoodsOrderDataChangeEvent event) {
        if (event.getStatus() == DataSetting.GOODS_ORDER_STATUS_CONFIRMED) {
            notifyDataChange();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUserLoginChange(UserLoginChangeEvent event) {
        notifyDataChange();
    }


    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        if (item instanceof GoodsOrderItemViewModel) {
            itemBinding.set(BR.viewModel, R.layout.layout_item_goods_order_confirmed).bindExtra(BR.presenter, viewGroupPresenter);
        }
    }

    @Override
    protected void onListItemClick(View view, BaseViewModel item) {
        super.onListItemClick(view, item);
        if (getActivity() == null) {
            showToast(R.string.error_activity_is_null);
            return;
        }
        if (item instanceof GoodsOrderItemViewModel) {
            switch (view.getId()) {
                case R.id.tv_execution:    //执行情况
                    break;
//                case R.id.rl_goods_content: //商品
//                    if (((GoodsOrderItemViewModel) item).goodsInfo == null
//                            || TextUtils.isEmpty(((GoodsOrderItemViewModel) item).goodsInfo.getGoodId())) {
//                        showToast(R.string.error_goods_is_empty);
//                        return;
//                    }
//                    GoodsDetailActivity.start(getActivity(), ((GoodsOrderItemViewModel) item).goodsInfo.getGoodId());
//                    break;
            }
        }
    }

    @Override
    public void refreshData(boolean init) {
        if (!UserData.getInstance().isLogin()) {
            showLogin();
            return;
        }
        super.refreshData(init);
        goodsDataRepository.getConfirmedGoodsOrderList(new DefaultObserver<List<GoodsOrder>>() {

            @Override
            public void onNext(List<GoodsOrder> goodsOrders) {
                List<GoodsOrderItemViewModel> list = new ArrayList<>();
                if (goodsOrders != null && !goodsOrders.isEmpty()) {
                    for (GoodsOrder item : goodsOrders) {
                        list.add(new GoodsOrderItemViewModel(item));
                    }
                }
                resetData(list);
            }

            @Override
            public void onError(MyException e) {
                setError(e.getMessage());
            }
        });
    }


}
